= form_for [@project, @merge_request], html: { class: "merge-request-form form-horizontal" } do |f|
  .row
    .col-sm-2
    .col-sm-10
      - if @repository.contribution_guide && !@merge_request.persisted?
        - contribution_guide_url = project_blob_path(@project, tree_join(@repository.root_ref, @repository.contribution_guide.name))
        .alert.alert-info
          Please review the
          %strong #{link_to "guidelines for contribution", contribution_guide_url}
          to this repository.

      -if @merge_request.errors.any?
        .alert.alert-danger
          - @merge_request.errors.full_messages.each do |msg|
            %div= msg

  .merge-request-branches
    .form-group
      = label_tag nil, class: 'control-label' do
        From
      .col-sm-10
        .clearfix
          .pull-left
            = f.select(:source_project_id, [[@merge_request.source_project_path,@merge_request.source_project.id]] , {}, { class: 'source_project select2 span3', disabled: @merge_request.persisted? })
          .pull-left
            &nbsp;
            = f.select(:source_branch, @merge_request.source_branches, { include_blank: "Select branch" }, {class: 'source_branch select2 span2'})
        .mr_source_commit
    %br
    .form-group
      = label_tag nil, class: 'control-label' do
        To
      .col-sm-10
        .clearfix
          .pull-left
            - projects =  @project.forked_from_project.nil? ? [@project] : [ @project,@project.forked_from_project]
            = f.select(:target_project_id, options_from_collection_for_select(projects, 'id', 'path_with_namespace', f.object.target_project_id), {}, { class: 'target_project select2 span3', disabled: @merge_request.persisted? })
          .pull-left
            &nbsp;
            = f.select(:target_branch, @merge_request.target_branches, { include_blank: "Select branch" }, {class: 'target_branch select2 span2'})
        .mr_target_commit

  %hr
  .merge-request-form-info
    .form-group
      = f.label :title, class: 'control-label' do
        %strong= "Title *"
      .col-sm-10= f.text_field :title, class: "form-control pad js-gfm-input", maxlength: 255, rows: 5, required: true
    .form-group
      = f.label :description, "Description", class: 'control-label'
      .col-sm-10
        = f.text_area :description, class: "form-control js-gfm-input", rows: 14
        %p.hint Description is parsed with #{link_to "GitLab Flavored Markdown", help_markdown_path, target: '_blank'}.

  .form-actions
    - if @merge_request.new_record?
      = f.submit 'Submit merge request', class: "btn btn-create"
    -else
      = f.submit 'Save changes', class: "btn btn-save"
    - if @merge_request.new_record?
      = link_to project_merge_requests_path(@source_project), class: "btn btn-cancel" do
        Cancel
    - else
      = link_to project_merge_request_path(@target_project, @merge_request), class: "btn btn-cancel" do
        Cancel

:javascript
  disableButtonIfEmptyField("#merge_request_title", ".btn-save");

  var source_branch = $("#merge_request_source_branch")
    , target_branch = $("#merge_request_target_branch")
    , target_project = $("#merge_request_target_project_id");

  $.get("#{branch_from_project_merge_requests_path(@source_project)}", {ref: source_branch.val() });
  $.get("#{branch_to_project_merge_requests_path(@source_project)}", {target_project_id: target_project.val(),ref: target_branch.val() });

  target_project.on("change", function() {
    $.get("#{update_branches_project_merge_requests_path(@source_project)}", {target_project_id:  $(this).val() });
  });
  source_branch.on("change", function() {
    $.get("#{branch_from_project_merge_requests_path(@source_project)}", {ref: $(this).val() });
  });
  target_branch.on("change", function() {
    $.get("#{branch_to_project_merge_requests_path(@source_project)}", {target_project_id: target_project.val(),ref: $(this).val() });
  });
